<template>
  <view id="productID">
    <view>
      <TableTitle></TableTitle>
    </view>
    <view class="tableContentClass">
      <transition-group name="fade" tag="ul">
        <template v-for="(item, index) in items" :key="index">
          <view class="tableClass" v-if="!item.isReplacing" @click="removeItem(item)">
            <li>{{ index + 1 }},</li>
            <li>{{ item.name }},</li>
            <li>{{ item.id }},</li>
          </view>
        </template>
      </transition-group>
    </view>
    <view class="pageClass" style="color: #fff !important">页码</view>
  </view>
</template>

<script setup lang="ts">
import TableTitle from '../TableTitle/TableTitle.vue'

const items = ref<{ id: number; name: string; isReplacing: boolean }[]>([
  { id: 1, name: 'Item 1', isReplacing: false },
  { id: 2, name: 'Item 2', isReplacing: false },
  { id: 3, name: 'Item 3', isReplacing: false },
  { id: 4, name: 'Item 4', isReplacing: false },
  { id: 5, name: 'Item 5', isReplacing: false },
  { id: 6, name: 'Item 6', isReplacing: false },
  { id: 7, name: 'Item 7', isReplacing: false },
  { id: 8, name: 'Item 8', isReplacing: false },
  { id: 9, name: 'Item 9', isReplacing: false },
  { id: 10, name: 'Item 10', isReplacing: false },
])
const nextId = ref(11)

function removeItem(item: any) {
  const index = items.value.findIndex((i) => i.id === item.id)
  if (index !== -1) {
    // 标记为正在替换
    items.value[index].isReplacing = true

    setTimeout(() => {
      // 移除旧项，并在同一位置添加新项
      items.value.splice(index, 1, {
        id: nextId.value,
        name: `New Item ${nextId.value}`,
        isReplacing: false,
      })
      nextId.value++
    }, 500) // 确保这个延时与.fade-leave-active的持续时间一致
  }
}

const inter = ref()
onMounted(() => {
  inter.value = setInterval(() => {
    let ran = Math.floor(Math.random() * 10)
    let id = items.value[ran]
    removeItem(id)
  }, 3000)
})
onUnmounted(() => {
  clearInterval(inter.value)
})
</script>

<style lang="scss" scoped>
#productID {
  height: 591.56px;
  width: 600.13px;
  background: url('../../static/images/Large_Content_Box.png') no-repeat;
  background-size: contain;
  position: relative;
  padding: 0 20px;

  .tableContentClass {
    margin-top: 30px;
    background: #0000;
    height: 520px;
    overflow-y: hidden;

    ul {
      padding-inline-start: 0 !important;
    }

    .tableClass {
      margin: 5px 0 !important;
      padding: 0 10px !important;
      background: url('../../static/images/Small_content_box.png') no-repeat;
      background-size: 100% auto;
      background-position: calc(50%);
      height: 47px;
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      align-items: center;

      li {
        color: #0ff;
        // height: 100%;
        display: inline-block;
      }
    }

    // :deep(.uni-table) {
    //   background-color: #fff0 !important;
    //   height: 520px;

    //   .uni-table-tr {
    //     margin: 5px 0 !important;
    //     background: url('../../static/images/Small_content_box.png') no-repeat;
    //     background-size: 100% auto;
    //     background-position: calc(50%);

    //     .uni-table-th,
    //     .uni-table-td {
    //       color: #0ff !important;
    //       border: none !important;
    //       box-sizing: border-box;
    //       // border-left: 1px #fff solid !important;
    //       // margin: 10px 0 !important;
    //       padding: 6px 10px !important;
    //       text-align: center;
    //       align-items: center;
    //     }

    //     .uni-table-td {
    //       .tdClass {
    //         height: 40px !important;
    //         display: flex;
    //         justify-content: center;
    //         align-items: center;
    //       }
    //     }

    //   }
    // }
  }
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.5s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0.1;
}
</style>
